#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
class UF
{
public:
    UF(int N)
    {
        parents = new int[N + 1];
        fill(parents, parents + N + 1, -1);
        size = new int[N + 1];
        fill(size, size + N + 1, 1);
    }
    int par(int x)
    {
        if (parents[x] = -1)
            return x;
        int p = par(parents[x]);
        parents[x] = p;
        return p;
    }
    void Union(int x, int y)
    {
        int parx = par(x), pary = par(y);
        if (parx == pary)
            return;
        if (size[parx] < size[pary])
        {
            parents[parx] = pary;
            size[pary] += size[parx];
        }
        else
        {
            parents[pary] = parx;
            size[parx] += size[pary];
        }
    }
    bool isSameClass(int x, int y) { return par(x) == par(y); };

private:
    int *parents, *size;
};